import { Effect, Reducer } from 'umi';

import { getUserInfo, query as queryUsers } from '@/services/user';

export interface CurrentUser {
  id?: string;
  name?: string;
  phone?: string;
  uid?: string; // 用户id
  type?: number; // 代理等级
  token?: string; // 登录token
  user_type?: string; // 用户类型
  auth?: UserAuth; // 用户权限
}

export interface UserAuth {
  aid: number;
  hide_use_rate: number;
  id: number;
  is_delete: number;
  oid: number;
  pc_end_order: number;
  pc_refund: number;
  seller_auth: number;
  seller_leasing_mode: number;
  seller_order: number;
  seller_pb_charge_info: number;
  seller_pl_charge_info: number;
  seller_profit: number;
  seller_rate_info: number;
  show_higher_profit: number; // 查看上级订单权限
}

export interface UserModelState {
  currentUser?: CurrentUser;
}

export interface UserModelType {
  namespace: 'user';
  state: UserModelState;
  effects: {
    fetch: Effect;
    fetchCurrent: Effect;
  };
  reducers: {
    saveCurrentUser: Reducer<UserModelState>;
    changeNotifyCount: Reducer<UserModelState>;
    saveCurrentUserToken: Reducer<UserModelState>;
  };
}

const UserModel: UserModelType = {
  namespace: 'user',

  state: {
    currentUser: {},
  },

  effects: {
    *fetch(_, { call, put }) {
      const response = yield call(queryUsers);
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *fetchCurrent(_, { call, put, select }) {
      const token = localStorage.getItem('agency_token');
      const user_type = localStorage.getItem('agency_user_type');
      const id = localStorage.getItem('agency_id');

      if (token && user_type && id) {
        // 更新state
        yield put({
          type: 'saveCurrentUserToken',
          payload: {
            id: id!,
            user_type: user_type!,
            token: token!,
            unset: true,
          },
        });

        // 获取数据
        const data = {
          uId: id,
          userType: user_type,
          token: token,
        };

        const response = yield call(getUserInfo, data);
        if (response.code == 0) {
          response.data.token = data.token;
          yield put({
            type: 'saveCurrentUser',
            payload: response.data,
          });
        }
      }
    },
  },

  reducers: {
    saveCurrentUserToken(state, { payload }) {
      if (!payload.unset) {
        localStorage.setItem('agency_id', payload.id);
        localStorage.setItem('agency_user_type', payload.user_type);
        localStorage.setItem('agency_token', payload.token);
      }
      return {
        ...state,
        currentUser: {
          token: payload.token,
          id: payload.id,
          user_type: payload.user_type,
        },
      };
    },
    saveCurrentUser(state, action) {
      return {
        ...state,
        currentUser: action.payload || {},
      };
    },
    changeNotifyCount(
      state = {
        currentUser: {},
      },
      action,
    ) {
      return {
        ...state,
        currentUser: {
          ...state.currentUser,
          notifyCount: action.payload.totalCount,
          unreadCount: action.payload.unreadCount,
        },
      };
    },
  },
};

export default UserModel;
